package cn.hp.controller;

import cn.hp.entity.Role;
import cn.hp.service.RoleService;
import cn.hp.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result find(Role role) {
        List<Role> list = roleService.selectRole(role);
        return Result.success(list);
    }
    @RequestMapping("/page")
    public Result page(Role role, @RequestParam(value="page", defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "10") Integer limit){
        PageInfo<Role> pageinfo = roleService.selectRole(role, page, limit);
        return Result.success(pageinfo);
    }
    @PostMapping
    public Result insert(@RequestBody Role role){
        int i = roleService.insertRole(role);
        return i > 0 ? Result.success("插入成功") : Result.error("插入失败");
    }
    @PutMapping
    public Result update(@RequestBody Role role) {
        int i = roleService.updateRole(role);
        return i > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        int i = roleService.deleteRoleById(id);
        return i > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("/getRole")
    public Object getMenu(Role obj) {
        List<Role> list = roleService.selectRole(obj);
        List<Map<String, Object>> Mmap = new ArrayList<>();
        for (Role m : list) {
            if ("0".equals(m.getStatus())) {
                Map map = new HashMap();
                map.put("id", m.getId());
                map.put("title", m.getRemark());
                Mmap.add(map);
            }
        }
        return Result.success(Mmap);
    }
}
